In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
from astropy import units as u
from astropy.coordinates import EarthLocation, SkyCoord
from astropy.time import Time
from astroplan.plots import plot_airmass, plot_sky, plot_sky_24hr, plot_finder_image
from matplotlib import pyplot as plt
%matplotlib inline
from astroplan import Observer
from pocs.scheduler.dispatch import Scheduler
from pocs.scheduler.constraint import Duration, MoonAvoidance
from pocs.utils.config import load_config
from pocs.utils import listify
config = load_config()
In [3]:
# Get a location
loc = config['location']
location = EarthLocation(lon=loc['longitude'], lat=loc['latitude'], height=loc['elevation'])
# Get an observer at the location
observer = Observer(location=location)
# Get the night hours (you can use observer.tonight())
times = observer.tonight(horizon=-18 * u.degree)
start_of_night = times[0]
end_of_night = times[1]
# start_of_night = Time('2016-08-13 10:00:00')
# end_of_night = Time('2016-08-13 16:00:00')
In [4]:
simple_fields_file = config['directories']['targets'] + '/simple.yaml'
# Simple constraint to maximize duration above a certain altitude
constraints = [MoonAvoidance(), Duration(30 * u.deg)]
# Create a scheduler
scheduler = Scheduler(observer, fields_file=simple_fields_file, constraints=constraints)
In [5]:
for obs in scheduler.observations.values():
print(obs)
In [6]:
# Get the best observations for the given time
# show_all returns entire list along with merit
best = scheduler.get_observation(time=start_of_night, show_all=True)
for b in listify(best):
print("Field: {:12s} Merit: {}".format(b[0], b[1]))
In [7]:
scheduler.current_observation.field.coord.equinox
Out[7]:
In [8]:
plot_airmass([scheduler.observations[name].field for name, merit in best],
scheduler.observer, start_of_night,
brightness_shading=True)
plt.axvline(start_of_night.plot_date, lw=2, ls='--', c='k')
plt.legend(loc=2)
plt.show()
In [9]:
plot_sky_24hr(scheduler.current_observation.field, scheduler.observer, start_of_night)
plt.show()
In [10]:
# Set a later time to start
t2 = end_of_night - 3 * u.hour
# Get the best observation for the given time
# show_all returns entire list along with merit
best = scheduler.get_observation(show_all=True, time=t2)
for b in best:
print("Field: {:12s} Merit: {}".format(b[0], b[1]))
In [11]:
plot_airmass([scheduler.observations[name].field for name, merit in best],
scheduler.observer, t2,
brightness_shading=True)
plt.axvline(t2.plot_date, lw=2, ls='--', c='k')
plt.legend(loc=2)
plt.show()
In [12]:
# Start at the beginning of the night
next_time = start_of_night
print("Start of night at {}".format(start_of_night))
# Reset scheduler
# Create a scheduler
scheduler = Scheduler(observer, fields_file=simple_fields_file, constraints=constraints)
while (next_time < end_of_night):
# Only get new target if we have met min_nexp for observation
if (scheduler.current_observation is None) or \
(scheduler.current_observation.current_exp >= scheduler.current_observation.min_nexp):
print("Getting new observation at {}".format(next_time))
new_obs = scheduler.get_observation(time=next_time)
if scheduler.current_observation is not None:
if (scheduler.current_observation.current_exp < scheduler.current_observation.min_nexp):
obs_duration = scheduler.current_observation.minimum_duration
# Fake the exposure count
scheduler.current_observation.current_exp = scheduler.current_observation.min_nexp
else:
obs_duration = scheduler.current_observation.set_duration
# Fake the exposure count
scheduler.current_observation.current_exp += scheduler.current_observation.exp_set_size
print("\tWill observe {} with merit of {:0.5f} for {}".format(
scheduler.current_observation.name,
scheduler.current_observation.merit,
obs_duration))
print("\t{} of {}".format(scheduler.current_observation.current_exp,
scheduler.current_observation.min_nexp))
next_time = next_time + obs_duration
print("End of night at {}".format(end_of_night.isot))
In [13]:
time = Time('2016-08-13 11:00:00')
scheduler.get_observation(time=time)
print(scheduler.current_observation)
time = Time('2016-08-13 13:00:00')
scheduler.get_observation(time=time)
print(scheduler.current_observation)
time = Time('2016-08-13 14:30:00')
scheduler.get_observation(time=time)
print(scheduler.current_observation)
In [14]:
scheduler.current_observation